package com.wudl.govern.controller;

import com.wudl.common.api.CommonResult;

import com.wudl.govern.domain.MetaIndex;
import com.wudl.govern.service.MetaIndexService;
import com.wudl.govern.vo.MetaIndexVO;
import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;
import java.util.ArrayList;

/**
 * meta_index Service
 *
 * @author wudl
 */
@Api
@RestController
@RequestMapping("/metaIndex")
public class MetaIndexController {

    @Autowired
    private MetaIndexService metaIndexService;

    /**
     * 分页查询
     */
    @PostMapping("/page")
    public CommonResult<List<MetaIndexVO>> page(@RequestBody MetaIndexVO pageVO, @RequestParam Integer page, @RequestParam Integer size) {

        List<MetaIndexVO> MetaIndexVoList = new ArrayList<>();
  // 拷贝结果
        List<MetaIndex> MetaIndexList = metaIndexService.page(page, size);

        for (MetaIndex metaIndexTmp:MetaIndexList )
        {
            MetaIndexVO metaIndexVO = new MetaIndexVO();
            BeanUtil.copyProperties(metaIndexTmp, MetaIndexVO.class);
            MetaIndexVoList.add(metaIndexVO);
        }
        return CommonResult.success(MetaIndexVoList);


    }

    /**
     * 查询详情
     */
    @GetMapping("/get/{id}")
    public CommonResult<MetaIndexVO> get(@PathVariable Object id) {
        MetaIndex entity = metaIndexService.getById(id);
        return CommonResult.success(BeanUtil.copyProperties(entity, MetaIndexVO.class));
    }

    /**
     * 新增
     */
    @PostMapping("/add")
    public CommonResult<Void> add(@RequestBody MetaIndexVO addVO) {
        metaIndexService.save(BeanUtil.copyProperties(addVO, MetaIndex.class));
        return CommonResult.success();
    }

    /**
     * 编辑
     */
    @PutMapping("/edit")
    public CommonResult<Void> edit(@RequestBody MetaIndexVO editVO) {
        metaIndexService.updateById(BeanUtil.copyProperties(editVO, MetaIndex.class));
        return CommonResult.success();
    }

    /**
     * 删除
     */
    @DeleteMapping("/delete/{id}")
    public CommonResult<Void> delete(@PathVariable Object id) {
        metaIndexService.removeById(id);
        return CommonResult.success();
    }

}
